// 理发项目路由
const express = require("express");
const path = require('path');
// const db = require('../db/index');
const HairProject = require("../models/hairProjects");
const router = new express.Router();

/* 
 接口设计

 三个接口 
 ①post接口  将收集到的数据提交到服务器 
    kind_id   name detail price

 ②get 查询全部数据
 ③get 携带参数查询部分数据
 
 */
// 中间件作用 先去连接数据
/* router.use('/', async (req, res, next) => {
    await db;
    next();
}) */
router.post("/hair_kind", async (req, res) => {
  console.log(req.body);
  // req.body中的数据放到数据库中
  const result = await HairProject.create(req.body);
  res.send("数据提交成功！！");
});

router.get("/hair_kind_search", async (req, res) => {
  console.log(req.query);
  const result = await HairProject.find(req.query, {
    _id: 0,
    __v: 0
  });
  // 解决跨域
  // res.setHeader('Access-Control-Allow-Origin', '*')
  res.send(result);
});
/* router.get('/project_two', async (req, res) => {

    const result = await HairProject.find(req.query, {
        _id: 0,
        __v: 0
    });
    res.send(result);
}) */
/* router.get('/project_two/:name', async (req, res) => {
    // req.params {}
    const result = await HairProject.find(req.params, {
        _id: 0,
        __v: 0
    });
    res.send(result);
}) */
console.log(__dirname);
let result = path.join(__dirname, '..', '..');
console.log(result);
module.exports = router;